home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / ljl.exe / PORTDIR.C < prev    next >
C/C++ Source or Header  |  1992-09-12  |  8KB  |  323 lines

  1.  
  2.  
  3. /***************************************************************************/
  4. /*                                                                         */
  5. /*                   Copyright (c) 1990, Bob Withers                       */
  6. /*                                                                         */
  7. /*  Files to allow portable access to directory and file structure info    */
  8. /*  across MSDOS and OS/2 compilers.                                       */
  9. /*                                                                         */
  10. /***************************************************************************/
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. #include "portdir.h"
  18.  
  19.  
  20. struct S_DateFmt
  21. {
  22.     unsigned short  day     : 5;
  23.     unsigned short  month   : 4;
  24.     unsigned short  year    : 7;
  25. };
  26. typedef struct S_DateFmt        DATEFMT;
  27.  
  28. struct S_TimeFmt
  29. {
  30.     unsigned short  twosecs : 5;
  31.     unsigned short  minutes : 6;
  32.     unsigned short  hours   : 5;
  33. };
  34. typedef struct S_TimeFmt        TIMEFMT;
  35.  
  36. union U_DateTimeCvt
  37. {
  38.     unsigned short  DateTime;
  39.     DATEFMT         Date;
  40.     TIMEFMT         Time;
  41. };
  42. typedef union U_DateTimeCvt     DATETIMECVT;
  43.  
  44.  
  45. static time_t CvtDosDateTime(unsigned short usDate, unsigned short usTime);
  46.  
  47.  
  48. #ifdef __TURBOC__
  49. #include <dir.h>
  50. #include <dos.h>
  51.  
  52. struct S_DirHandle
  53. {
  54.     short           sCnt;
  55.     unsigned        uAttr;
  56.     struct ffblk    ff;
  57.     DIRECTENTRY     DirectEntry;
  58.     char            path[1];
  59. };
  60. typedef struct S_DirHandle        DIRHANDLE;
  61.  
  62.  
  63. void *DirOpen(char *pathname, unsigned uAttr)
  64. {
  65.     auto     DIRHANDLE      *pDirHandle;
  66.  
  67.     pDirHandle = malloc(sizeof(DIRHANDLE) + strlen(pathname));
  68.     if (pDirHandle)
  69.     {
  70.         pDirHandle->sCnt  = 0;
  71.         pDirHandle->uAttr = uAttr;
  72.         strcpy(pDirHandle->path, pathname);
  73.     }
  74.  
  75.     return(pDirHandle);
  76. }
  77.  
  78.  
  79. DIRECTENTRY *DirRead(void *pHandle)
  80. {
  81.     auto     int        nRC;
  82.     auto     DIRHANDLE     *pDirHandle;
  83.  
  84.     if (NULL == pHandle)
  85.         return(NULL);
  86.  
  87.     pDirHandle = pHandle;
  88.  
  89.     if (0 == pDirHandle->sCnt++)
  90.         nRC = findfirst(pDirHandle->path, &pDirHandle->ff,
  91.                         pDirHandle->uAttr);
  92.     else
  93.         nRC = findnext(&pDirHandle->ff);
  94.  
  95.     if (0 != nRC)
  96.         return(NULL);
  97.  
  98.     pDirHandle->DirectEntry.d_attr     = pDirHandle->ff.ff_attrib;
  99.     pDirHandle->DirectEntry.d_datetime =
  100.                     CvtDosDateTime(pDirHandle->ff.ff_fdate,
  101.                                    pDirHandle->ff.ff_ftime);
  102.     pDirHandle->DirectEntry.d_filesize = pDirHandle->ff.ff_fsize;
  103.     pDirHandle->DirectEntry.d_namlen   = strlen(pDirHandle->ff.ff_name);
  104.  
  105.     strcpy(pDirHandle->DirectEntry.d_name, pDirHandle->ff.ff_name);
  106.  
  107.     return(&pDirHandle->DirectEntry);
  108. }
  109.  
  110.  
  111. void DirClose(void *pHandle)
  112. {
  113.     free(pHandle);
  114.     return;
  115. }
  116.  
  117. #endif    /* __TURBOC__ */
  118.  
  119.  
  120. #ifdef _MSC_VER
  121. #ifdef OS2
  122.  
  123. #define INCL_BASE
  124. #include <os2.h>
  125.  
  126. struct S_DirHandle
  127. {
  128.     SHORT       sCnt;
  129.     USHORT      uAttr;
  130.     HDIR        hDir;
  131.     DIRECTENTRY DirectEntry;
  132.     CHAR        path[1];
  133. };
  134. typedef struct S_DirHandle       DIRHANDLE;
  135.  
  136.  
  137. void *DirOpen(char *pathname, unsigned uAttr)
  138. {
  139.     auto     DIRHANDLE     *pDirHandle;
  140.  
  141.     pDirHandle = malloc(sizeof(DIRHANDLE) + strlen(pathname));
  142.     if (pDirHandle)
  143.     {
  144.         pDirHandle->sCnt  = 0;
  145.         pDirHandle->uAttr = uAttr;
  146.         pDirHandle->hDir  = HDIR_CREATE;
  147.         strcpy(pDirHandle->path, pathname);
  148.     }
  149.  
  150.     return(pDirHandle);
  151. }
  152.  
  153.  
  154. DIRECTENTRY *DirRead(void *pHandle)
  155. {
  156.     auto     USHORT           usRC;
  157.     auto     USHORT           usSearchCnt = 1;
  158.     auto     FILEFINDBUF      ff;
  159.     auto     DIRHANDLE       *pDirHandle;
  160.     auto     DATETIMECVT      Cvt;
  161.     auto     unsigned short   usDate, usTime;
  162.  
  163.     if (NULL == pHandle)
  164.         return(NULL);
  165.  
  166.     pDirHandle = pHandle;
  167.  
  168.     if (0 == pDirHandle->sCnt++)
  169.         usRC = DosFindFirst(pDirHandle->path, &pDirHandle->hDir,
  170.                             pDirHandle->uAttr, &ff, sizeof(ff),
  171.                             &usSearchCnt, 0L);
  172.     else
  173.         usRC = DosFindNext(pDirHandle->hDir, &ff, sizeof(ff), &usSearchCnt);
  174.  
  175.     if (usRC || 0 == usSearchCnt)
  176.         return(NULL);
  177.  
  178.     pDirHandle->DirectEntry.d_attr = ff.attrFile;
  179.  
  180.     Cvt.Date.day   = ff.fdateLastWrite.day;
  181.     Cvt.Date.month = ff.fdateLastWrite.month;
  182.     Cvt.Date.year  = ff.fdateLastWrite.year;
  183.     usDate = Cvt.DateTime;
  184.  
  185.     Cvt.Time.twosecs = ff.ftimeLastWrite.twosecs;
  186.     Cvt.Time.minutes = ff.ftimeLastWrite.minutes;
  187.     Cvt.Time.hours   = ff.ftimeLastWrite.hours;
  188.     usTime = Cvt.DateTime;
  189.  
  190.     pDirHandle->DirectEntry.d_datetime = CvtDosDateTime(usDate, usTime);
  191.  
  192.     pDirHandle->DirectEntry.d_filesize = ff.cbFile;
  193.     pDirHandle->DirectEntry.d_namlen   = ff.cchName;
  194.  
  195.     memcpy(pDirHandle->DirectEntry.d_name, ff.achName, ff.cchName);
  196.     pDirHandle->DirectEntry.d_name[ff.cchName] = '\0';
  197.  
  198.     return(&pDirHandle->DirectEntry);
  199. }
  200.  
  201.  
  202. void DirClose(void *pHandle)
  203. {
  204.     free(pHandle);
  205.     return;
  206. }
  207.  
  208. #else     /*  MSDOS  */
  209.  
  210. #include <direct.h>
  211. #include <dos.h>
  212.  
  213. struct S_DirHandle
  214. {
  215.     short           sCnt;
  216.     unsigned        uAttr;
  217.     struct find_t   ff;
  218.     DIRECTENTRY     DirectEntry;
  219.     char            path[1];
  220. };
  221. typedef struct S_DirHandle        DIRHANDLE;
  222.  
  223.  
  224. void *DirOpen(char *pathname, unsigned uAttr)
  225. {
  226.     auto     DIRHANDLE     *pDirHandle;
  227.  
  228.     pDirHandle = (DIRHANDLE *) malloc(sizeof(DIRHANDLE) + strlen(pathname));
  229.     if (pDirHandle)
  230.     {
  231.         pDirHandle->sCnt  = 0;
  232.         pDirHandle->uAttr = uAttr;
  233.         strcpy(pDirHandle->path, pathname);
  234.     }
  235.  
  236.     return(pDirHandle);
  237. }
  238.  
  239.  
  240. DIRECTENTRY *DirRead(void *pHandle)
  241. {
  242.     auto     unsigned         uRC;
  243.     auto     DIRHANDLE       *pDirHandle;
  244.  
  245.     if (NULL == pHandle)
  246.         return(NULL);
  247.  
  248.     pDirHandle = (DIRHANDLE *) pHandle;
  249.  
  250.     if (0 == pDirHandle->sCnt++)
  251.         uRC = _dos_findfirst(pDirHandle->path, pDirHandle->uAttr,
  252.                              &pDirHandle->ff);
  253.     else
  254.         uRC = _dos_findnext(&pDirHandle->ff);
  255.  
  256.     if (0 != uRC)
  257.         return(NULL);
  258.  
  259.     pDirHandle->DirectEntry.d_attr     = pDirHandle->ff.attrib;
  260.     pDirHandle->DirectEntry.d_datetime =
  261.                 CvtDosDateTime(pDirHandle->ff.wr_date,
  262.                                pDirHandle->ff.wr_time);
  263.     pDirHandle->DirectEntry.d_filesize = pDirHandle->ff.size;
  264.     pDirHandle->DirectEntry.d_namlen   = strlen(pDirHandle->ff.name);
  265.  
  266.     strcpy(pDirHandle->DirectEntry.d_name, pDirHandle->ff.name);
  267.  
  268.     return(&pDirHandle->DirectEntry);
  269. }
  270.  
  271.  
  272. void DirClose(void *pHandle)
  273. {
  274.     free(pHandle);
  275.     return;
  276. }
  277.  
  278. #endif    /* OS2 */
  279. #endif    /* _MSC_VER */
  280.  
  281. static time_t CvtDosDateTime(unsigned short usDate, unsigned short usTime)
  282. {
  283.     auto     DATETIMECVT        dt;
  284.     auto     struct tm          tm;
  285.  
  286.     dt.DateTime = usTime;
  287.     tm.tm_sec   = dt.Time.twosecs * 2;
  288.     tm.tm_min   = dt.Time.minutes;
  289.     tm.tm_hour  = dt.Time.hours;
  290.  
  291.     dt.DateTime = usDate;
  292.     tm.tm_mday  = dt.Date.day;
  293.     tm.tm_mon   = dt.Date.month;
  294.     tm.tm_year  = dt.Date.year + 80;
  295.  
  296.     tm.tm_wday  = 0;
  297.     tm.tm_yday  = 0;
  298.     tm.tm_isdst = 0;
  299.  
  300.     return(mktime(&tm));
  301. }
  302.  
  303.  
  304. /*  UNIX compatibility  */
  305.  
  306. DIR *opendir(char *pathname)
  307. {
  308.     return(DirOpen(pathname, FILEATTR_NORMAL));
  309. }
  310.  
  311.  
  312. struct dirent *readdir(DIR *pDir)
  313. {
  314.     return((struct dirent *) DirRead(pDir));
  315. }
  316.  
  317.  
  318. void closedir(DIR *pDir)
  319. {
  320.     DirClose(pDir);
  321.     return;
  322. }
  323.